﻿using System;
using System.Diagnostics;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Globalization;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;


namespace MVAScratchPad
{
    /// <summary>
    ///     Provides application-specific behavior to supplement the default Application class.
    /// </summary>
    sealed partial class App : Application
    {
        public App()
        {
            this.InitializeComponent();
        }

        private Frame Frame
        {
            get
            {
                Frame rootFrame = Window.Current.Content as Frame;
                if (rootFrame == null)
                {
                    rootFrame = new Frame();
                    Window.Current.Content = rootFrame;
                }
                return rootFrame;
            }
        }

        protected override void OnLaunched(LaunchActivatedEventArgs args) { Activate(args); }
        protected override void OnActivated(IActivatedEventArgs args) { Activate(args); }
        protected override void OnSearchActivated(SearchActivatedEventArgs args) { Activate(args); }
        void Activate(IActivatedEventArgs e)
        {
            switch (e.Kind)
            {
                case ActivationKind.Launch:
                    {
                        var args = e as ILaunchActivatedEventArgs;
                        if (args.PreviousExecutionState == ApplicationExecutionState.Running)
                            this.Frame.Navigate(typeof(Views.Splash));
                        else
                        {
                            // do not activate, let the splash activate itself
                            var page = new Views.Splash()
                            {
                                SplashScreen = args.SplashScreen,
                                Navigate = () => this.Frame.Navigate(typeof(Views.MainPage))
                            };
                            Window.Current.Content = page as UIElement;
                            page.Start();
                        }
                        break;
                    }
                default:
                    throw new NotImplementedException(e.Kind.ToString());
            }
        }
    }
}